/**
 * 
 */
package com.heer.RD.projecthour.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.model.Model;
import org.springframework.jdbc.core.JdbcTemplate;

import com.heer.RD.projecthour.pojo.ProjectDTO;

/**
 * @author ZhaoZehui
 *
 */
public class StaffChoice extends ListChoice {

	private Map<String, String> NAMES = new HashMap<String, String>();
	private List<String> userIds = new ArrayList<String>();
	
	public StaffChoice(String id) {
		super(id);
		setMaxRows(1);
		setModel(new Model<String>());
		setChoices(userIds);
	}

	public void getStaffDirectly(JdbcTemplate jt, String userCondStr) {
		NAMES.clear();
		userIds.clear();
		
		String sql = "select user_id,name from user_info where is_used = 0 and " + userCondStr;
		
		List<Map<String, String>> pList = jt.queryForList(sql);
		for (Map<String, String> proj : pList) {
			userIds.add(proj.get("USER_ID"));
			NAMES.put(proj.get("USER_ID"), proj.get("NAME"));
		}
		
		IChoiceRenderer render = new ChoiceRenderer() {
			public Object getDisplayValue(Object object) {
				return NAMES.get(object);
			}
			public String getIdValue(Object object, int index)	{
				return String.valueOf(userIds.get(index));
			}
		};

		this.setChoices(userIds);
		this.setChoiceRenderer(render);
	}
	
	public void getUsersByDept(JdbcTemplate jt, String deptId) {
		NAMES.clear();
		userIds.clear();

		if(StringUtils.isNotBlank(deptId)) {
		
			String sql = "select user_id,name from user_info where dept_id = ? order by name";
			
			List<Map<String, String>> pList = jt.queryForList(sql, new Object[]{deptId});
			for (Map<String, String> proj : pList) {
				userIds.add(proj.get("USER_ID"));
				NAMES.put(proj.get("USER_ID"), proj.get("NAME"));
			}
		}
		
		IChoiceRenderer render = new ChoiceRenderer() {
			public Object getDisplayValue(Object object) {
				return NAMES.get(object);
			}
			public String getIdValue(Object object, int index)	{
				if(index >= 0)
					return String.valueOf(userIds.get(index));
				else
					return "";
			}
		};

		this.setChoices(userIds);
		this.setChoiceRenderer(render);
	}
	
	public String getStaffName(String userId) {
		return NAMES.get(userId);
	}
}
